> Program: SCROLL-1 - Visual Basic Demo Code > Written By: Paul T. Dawson, P.O. Box 682, Chincoteague, VA, 23336 > Summary: VB 2.0 or 3.0 sample code that demonstrates "form scrolling". No VBX controls required. No API calls required. No cost (free!). > Requirements: To run the SCROLL-1.EXE file - Windows 3.x and VBRUN200.DLL. To use the sample code - Visual Basic 2.0 or higher. > Design Goals: 1. Make a resizable form, with scroll bars to move around a large area. 2. Use the minimum amount of code. 3. Use default values wherever possible. 4. Add lots of comments! > File List: SCROLL-1.TXT - This File (no margins or page breaks). SCROLL-1.MAK - Visual Basic Project. SCROLL-1.FRM - Visual Basic Form. SCROLL-1.FRX - Visual Basic Graphics (just one little icon). SCROLL-1.EXE - Finished EXE file. > Design Summary: This program uses one standard resizable form, and a "scrolling" picture box for the "container" for all of the controls. There are two scroll bars on the form that let the user scroll around the picture box. The program moves the scroll bars to their new locations every time the main form is resized. Then the user can scroll anywhere in the picture box. The scrolling picture box can be many times larger than the screen, and any control can be placed anywhere on it. > Details About Objects: 1. frmMain: The main form has a ScaleMode of 3 (pixel). Everything else on the form is default. 2. hsb1 / vsb1: The two scroll bars are in temporary locations on the form. They will be automatically moved to the edges when you click cmdStart. The values of SmallChange and LargeChange are 10 and 50. These values are not critical, and they can be changed. Everything else is default. 3. picCorner: This is the little grey box that goes into the lower right corner. The BorderStyle is 1, and the BackColor is light grey. You can change the BackColor to something else, if you want to see exactly where picCorner lands! 4. picBig: This is the big scrolling picture box. In this demo, it starts out relatively small. Then cmdStart resizes it to be larger than the form. The picBig AutoRedraw property is set to False, which is the default. If you set the picBig AutoRedraw to True, then it will be easier to print and draw graphics directly on picBig. However, AutoRedraw=True uses a LOT of memory, so it's best to leave it False! With AutoRedraw=False, I have successfully tested this up to 16,383 by 16,383 pixels, the equivalent of 873 standard VGA screens. Above that number, which is (2^14)-1, things start to get flakey. The picture box will not display properly, most likely because of some internal VB restriction! On my machine, VB says 16,383 pixels is 170 inches, or just over 14 feet. So you can have a scrolling picture box that covers 200 actual square feet. That should be sufficient! The picBig ScaleMode is set to 3 (pixel) - this is important for this demo program, and it's just a lot more convenient than "Twips". You can always use Twips in other programs, if you really want to! After putting the scroll bars and the picture boxes on the form, it's important to select the picBig box and hit "Edit - Send to Back", so it won't ever cover the scroll bars or the picCorner box. The picBig BackColor is cyan - this isn't critical. 5. lblDownLeft / lblDownRight / lblUpLeft / lblUpRight: The four labels on the picBig control are just samples, to show how all controls in a "container" keep the same location in the container, even if the container moves around, or part of it is hidden. 6. cmdStart: When you click this, everything starts to happen! This button was added for the convenience of people without VB who just want to try the EXE file. It just makes things more interesting to see all of the controls before they are resized. 7. cmdExit(0 to 7): These are some extra non-critical controls that I added to picBig, to make the scrolling more visible. For a "real" program, these would all be replaced with "real" controls that actually did something. Actually, in this program, all of these buttons do something - end the program! > Miscellaneous Notes: 1. The horizontal scroll bar can be any height, and the vertical one can be any width. Try changing these, and you'll see that the code adjusts EVERYTHING to match! 2. Remember, any type of control can be placed onto the picBig control!!! 3. In writing this program, I found that the first EASY part was getting the scroll bars to jump to the edges after every form resize. The second EASY part was using the scroll bars to move the big picture box around. The completely unexpected HARD part was taking care of problems when just one scroll bar is visible!?! It's easy to place things when both scroll bars are either visible or invisible. However, when just one is visible, things get complicated! For example, if the entire width of picBig is visible, then the horizontal scroll bar can be turned off. Well, when you remove that from the bottom of the form, right away you have to check if the entire height of picBig is visible! The opposite is when the entire height is visible - then you turn off the vertical scroll bar, and you have to check if that makes the entire width of picBig visible! In either case, you have to erase the picCorner box, and then adjust the scroll bar again to fill in that space - whew! The solution that I finally used was to set up three flags, for the one corner box and the two scroll bars. The Form_Resize event will call the FixScrollBars sub, where these flags are all set to False, and then with some repetitive lines (all 16 of them), none/some/all of these flags are set to True. Then all three controls are put in the right places, and turned on or off depending on what the flags say! This system will always work properly. While testing this program, I put a loop around those lines, so instead of checking once, it checked 1000 times. The total time required was 18 seconds, or .018 second for just one "normal" check. That's fast enough (tested on a slow 386 even)! 4. In the FixScrollBars sub, there are several "+1" and "+2" variations in the sizes of the scroll bars. These are necessary to make everything line up perfectly. I used "ZoomIn" to look closely at each combination, and make sure everything was exact! 5. For VB beginners, here's how to put new controls INTO a container: First, select the container (usually a picture box or frame). Then, while it is selected, move over to the toolbox and click on the control you want. Move the mouse pointer back over the container, and THEN hold down the button, drag, and release. The new control should be inside the container now. To make sure, select it and try to drag it off the container. If you can't, then the operation was successful! Having a control IN a container is completely different than having a control ON TOP of a container. If you create a control and then drag it on top of a picture box, it will NOT be "in" that container. If you move the picture box, the control will NOT move with it! To move an existing control INTO a container, here's what to do: Select the control, and hit "ALT E" and "T". This will cut the control, which means it will disappear from the form. Then select the container, and while it is selected, hit "CTRL V" and the control will be pasted into the container. 6. Please experiment on non-critical projects first! /**** - <10-29-93> - ****/